package com.xiaoy.core.database.form.action;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Results;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.xiaoy.core.database.entity.DataTable;
import com.xiaoy.core.database.form.entity.View;
import com.xiaoy.core.database.form.server.FetterService;
import com.xiaoy.core.database.form.server.ViewService;
import com.xiaoy.core.database.form.vo.ViewTemplateData;
import com.xiaoy.core.database.service.DatabaseService;
import com.xiaoy.web.BasicAction;

import freemarker.template.Template;
import freemarker.template.TemplateException;

@Namespace("/fetter")
@Results( {
})
public class FetterAction extends BasicAction {

	@Resource(name="databaseService")
	private DatabaseService service ;
	@Resource
	private FetterService fetterService;
	@Resource
	private ViewService viewService;
	
	
	public void dataTablesList (){
		renderJson(service.getDataTables(initPageWrapper().getFilters()));
	}
	
	public void dataTableFieldByTableName () {
		
		String tablename = getParameter("tablename");
		renderJson(service.getDBColumnsByTablename(tablename));
	}
	
	
//----------------------------
	
	public String dataTableList () {
		request.setAttribute("listData", service.getDataTables());
		return "dataTableList";
	}
	
	public String dataTablesWithField () {
		
		String[] tables = getParameters("tables");
		
//		service.getDBColumnsByTablename(tablename);
		List <DataTable> dataTables = Lists.newArrayList();
		for (String tablename : tables) {
			dataTables.add(service.getDBTableInfo(tablename));
		}
		request.setAttribute("dataTables", dataTables);
		return "dataTablesWithField";
	}
	
	// 出预览页面
	public String parsing () {
		
		String type = getParameter("type");	//
		try {
			if ("view".equals(type)) {
				
					parsingView();
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "parsing";
	}
	
	
	private ViewTemplateData parsingView () throws Exception {
		long id = getParameterLong("viewid");
//		Map m = filterParametersPrefix(request, "filter_");
		return fetterService.parsing(new View(id));
	}
	
//	SELECT ID, COMPANY_ID, POST_ID, NAME, JOB_NATURE, PROVINCE_ID, CITY_ID, AREA_ID, RECRUIT_COUNT, DEGREE, WORK_EXPERIENCE, GENDER, AGE_RANGE, SALARY_RANGE, PROFESSION, START_DATE, END_DATE, STATUS, CONTACTS, TEL, MOBILE, EMAIL, IS_PUBLIC, PUBLIC_DATE, PUBLIC_USER, REMARK FROM BUS_COMPANY_POSITION WHERE ID = ? AND COMPANY_ID = ?  AND SALARY_RANGE = ? AND AGE_RANGE = ? AND NAME=?  AND area_id=? ORDER BY PUBLIC_DATE DESC
	
	
	
	public void free () {
		
		try {
			Map map = Maps.newHashMap();
			map.put("name", "nimei");
			String htmlName = "cty.html";
			
			Template template=freeMarkerCfg.getTemplate("test.html");
			
			template.setEncoding("utf-8");
			
			String path= session.getServletContext().getRealPath("/");
			BufferedWriter buff= new BufferedWriter(new FileWriter(path+htmlName));
			File htmlFile = new File(path+htmlName);
			Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(htmlFile), "utf-8"));	
			
			PrintWriter pw = response.getWriter();
			
			template.process(map,response.getWriter());
			response.getWriter().flush();
			buff.close();
//			out.flush();
//			out.close();
			
		} catch (IOException e) {
			e.printStackTrace();
		} catch (TemplateException e) {
			e.printStackTrace();
		}
//		return "free";
	}
	
}


